// 回复内容的模块
const tip = '感谢您关注该测试号'
export default async (ctx, next) => {
  const message = ctx.weixin
  console.log(message)
  let mp = require('../wechat')
  let client = mp.getWechat()
  //const data = await client.handle('fetchMaterial', 'video', 'GuBt6sdCeeC8KvQ49ShGmJKBIB-7hniP5TRgdojVYE3XrtZN_6K-DlGnh4Ch4Olx')
  //const data = await client.handle('uploadMaterial', 'image', resolve(__dirname, '../../1.jpg'), {type: 'image'} )
  //const data = await client.handle('uploadMaterial', 'news', news, {} )
  //const data = await client.handle('fetchMaterial', 'news','t6aK29Oj0KJQvE1_PMlLcVFeSfeEYroPm72W9leoIxo', {})
  //const data = await client.handle('deleteMaterial', 't6aK29Oj0KJQvE1_PMlLcWaCsLud67s687uVpJmm94o')
  //const data = await client.handle('updataMaterial', 't6aK29Oj0KJQvE1_PMlLcVFeSfeEYroPm72W9leoIxo', updateNew)
  //const data = await client.handle('fetchCount')
  //const data = await client.handle('fetchBatch',{})

  if (message.MsgType === 'event') {
    if (message.Event === 'subscribe') {
      ctx.body = tip
    } else if (message.Event === 'unsubscribe') {
      console.log('取消关注le')
    } else if (message.Event === 'LOCATION') {
      ctx.body = message.Latitude + ':' + message.Longitude
    } else if (message.MsgType === 'view') {
      ctx.body = message.EventKey + message.MenuID
    } else if (message.MsgType === 'pic_sysphoto') {
      ctx.body = message.Count + 'photot sent'
    }
  } else if (message.MsgType === 'text') {
    if (message.Content === '1') {
      const data = await client.handle('fetchMaterial', 'news', 't6aK29Oj0KJQvE1_PMlLcVFeSfeEYroPm72W9leoIxo', {})
      // ctx.body =  {
      //   type: 'news',
      //   content: data.news_item,

      // }
      
    }
    if (message.Content === '2') {
      const data = await client.handle('getUserList')
      console.log(data)
    }
    if (message.Content === '3') {
      const data = await client.handle('getUserInfo', 'oiS5Jwn0dNRBwdQU_d3Bf5IydYtA')
      console.log(data)
    }
    if (message.Content === '4') {
      const menu = require('./menu').default
      await client.handle('delMenu')
      await client.handle('createMenu', menu)
      const data = await client.handle('getMenu')
      console.log(data)
    }

    // ctx.body = message.Content
  } else if (message.MsgType === 'image') {
    ctx.body = {
      type: 'image',
      mediaId: message.MediaId
    }
  } else if (message.MsgType === 'voice') {
    ctx.body = {
      type: 'voice',
      mediaId: message.MediaId
    }
  } else if (message.MsgType === 'video') {
    ctx.body = {
      title: message.ThumbMediaId,
      type: 'video',
      mediaId: message.MediaId
    }
  } else if (message.MsgType === 'location') {
    ctx.body = message.Location_X + ':' + message.Location_Y + ':' + message.Label
  } else if (message.MsgType === 'link') {
    ctx.body = [{
      title: message.Title,
      description: message.Descript,
      picUrl: 'baidu.com',
      url: message.Url
    }]
  } else {
    // ctx.body = tip
  }
}